/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 *
 * @author Ripper
 */
public class ViewLobbyPanel extends ClientPanel implements ActionListener, ListSelectionListener
{
    private String[] activeSessions;
    private String[] inactiveSessions;
    private JList activeSessionList;
    private JList inactiveSessionList;
    
    private JButton joinSession;
    private JButton returnButton;
    private JButton createSessionButton;
    
    public ViewLobbyPanel(ClientWindow parent)
    {
        super(parent);
        setLayout(new BorderLayout());
        
        JPanel midPanel = new JPanel(new BorderLayout());
        {
            JPanel grid = new JPanel(new GridLayout(2,1));
            {
                JPanel activePanel = new JPanel(new BorderLayout());
                activePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                {
                    activePanel.add(new JLabel(" Active Sessions: "), BorderLayout.NORTH);
                    
                    activeSessionList = new JList();
                    activeSessionList.addListSelectionListener(this);
                    activePanel.add(activeSessionList, BorderLayout.CENTER);
                }
                grid.add(activePanel);
                JPanel inactivePanel = new JPanel(new BorderLayout());
                inactivePanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
                {
                    inactivePanel.add(new JLabel(" Inactive Sessions: "), BorderLayout.NORTH);
                    
                    inactiveSessionList = new JList();
                    inactiveSessionList.addListSelectionListener(this);
                    inactivePanel.add(inactiveSessionList, BorderLayout.CENTER);
                }
                grid.add(inactivePanel);
            }
            midPanel.add(grid, BorderLayout.CENTER);
        }
        add(midPanel, BorderLayout.CENTER);
        
        JPanel bottom = new JPanel(new GridLayout(1,3));
        {
            joinSession = new JButton(" Join session ");
            joinSession.addActionListener(this);
            bottom.add(joinSession);
            
            returnButton = new JButton(" Return ");
            returnButton.addActionListener(this);
            bottom.add(returnButton);
            
            createSessionButton = new JButton(" Create Session ");
            createSessionButton.addActionListener(this);
            bottom.add(createSessionButton);
        }
        add(bottom, BorderLayout.SOUTH);
    }
    
    public void fillWithData(String[] activeSessions, String[] inactiveSessions)
    {
        if(activeSessions != null)
        {
            this.activeSessions = activeSessions;
            String[] newActive = new String[activeSessions.length];
            for(int a = 0;a < activeSessions.length; a++)
            {
                String dat = activeSessions[a].split(",")[0] + " ("+
                             activeSessions[a].split(",")[1] + " players)";

                newActive[a] = dat;
            }
            activeSessionList.setListData(newActive);
        }
        else
        {
            String[] empty = new String[0];
            activeSessionList.setListData(empty);
        }
        
        if(inactiveSessions != null)
        {
            this.inactiveSessions = inactiveSessions;
            String[] newInactive = new String[inactiveSessions.length];

            for(int a = 0;a < inactiveSessions.length; a++)
            {
                String dat = inactiveSessions[a].split(",")[0] + " ("+
                             inactiveSessions[a].split(",")[1] + " players)";

                newInactive[a] = dat;
            }

            inactiveSessionList.setListData(newInactive);
        }
        else
        {
            String[] empty = new String[0];
            inactiveSessionList.setListData(empty);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == joinSession)
        {
            if(inactiveSessionList.getSelectedIndex() != -1)
            {
                //int index = inactiveSessionList.getSelectedIndex();
                
                String sessionName = inactiveSessionList.getSelectedValue().toString();
                int a = sessionName.lastIndexOf('(');
                sessionName = sessionName.substring(0, a - 1);
                
                getClientWindow().connectToSession(sessionName);
            }
            else if(activeSessionList.getSelectedIndex() != -1)
            {
                //int index = activeSessionList.getSelectedIndex();
                
                String sessionName = activeSessionList.getSelectedValue().toString();
                int a = sessionName.lastIndexOf('(');
                sessionName = sessionName.substring(0, a - 1);
                
                getClientWindow().connectToSession(sessionName);
            }
        }
        else if(e.getSource() == returnButton)
        {
            getClientWindow().gotoWelcomeScreen();
        }
        else if(e.getSource() == createSessionButton)
        {
            String sessionName = JOptionPane.showInputDialog("Enter session name: ");
            
            getClientWindow().createNewSession(sessionName);
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) 
    {
        if(e.getSource() == activeSessionList)
        {
            inactiveSessionList.clearSelection();
        }
        else if(e.getSource() == inactiveSessionList)
        {
            activeSessionList.clearSelection();
        }
    }
}
